<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <div class="letf">欢迎来到叩丁狼积分商城</div>
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" width="24px" alt="" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isShowLogin">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <s>{{ cartTotal }}</s>
        </li>
        <li class="logo-btn" @click="changIsShowLoginModal" v-else>登录</li>
      </ul>
    </div>
  </div>
</template>
 
<script>
/* 
  1.布局一个购物车按钮
  2.vuex中loginStatus存储一个状态isLogined(是否登录的状态)
    store
      loginStatus
        index.js
  3.根据isLogined来控制购物车按钮是否显示
    本地有token isLogined则为true，本地无token则为false
  最终效果，刷新后可以根据本地是否有token来控制是否显示购物车按钮
*/
import { mapActions, mapMutations, mapState } from "vuex";
import { wechatPCLoginAPI } from "@/request/api";
export default {
  methods: {
    ...mapMutations({
      changIsShowLoginModal: "showModal/changIsShowLoginModal",
      changeIsShowLogin: "loginStatus/changeIsShowLogin",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncchangeIsShowToast: "showToast/asyncchangeIsShowToast",
      asyncUpdateUserInfo: "userInfo/asyncUpdateUserInfo",
    }),
  },
  computed: {
    ...mapState({
      isShowLogin: (state) => state.loginStatus.isShowLogin,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  async created() {
    // console.log("tobarKey进来了");
    // 微信登录第二步：临时票据code换取token
    let code = this.$route.query.code;
    if (code) {
      let res = await wechatPCLoginAPI({
        code,
      });
      // console.log("微信扫码登录res:", res);
      if (res.code == 0) {
        // 1.提示用户登录成功
        this.asyncchangeIsShowToast({
          isShow: true,
          msg: "登录成功",
          type: "success",
        });
        // 2.保存token值
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 3.自动刷新购物车按钮
        this.changeIsShowLogin(true);
        // 4.清除地址栏code
        this.$route.query.code && this.$router.push(this.$route.path);
      } else if (res.code == 400) {
        // 1.提示用户二维码失效
        this.asyncchangeIsShowToast({
          // 二维码失效
          isShow: true,
          msg: "二维码失效,请重新扫码",
          type: "warning",
        });
        // 改变登录状态
        this.changIsShowLoginModal(true);
      } else if (res.code == 407) {
        // 1.用户的微信还没有在这个网站上绑定过手机
        // 使用手机号登录并携带uuid，由后端自动绑定
        this.asyncchangeIsShowToast({
          isShow: true,
          msg: "请绑定手机号码再次登录",
          type: "danger",
        });
        // 3.把登录模态窗口打开
        this.changIsShowLoginModal(true);
        // 4.保存uuid
        localStorage.setItem("uuid", res.uuid);
      }
    } else {
      // ！！！如果不是登录进来的，就会执行这里的代码
      let token = localStorage.getItem("x-auth-token");
      this.changeIsShowLogin(Boolean(token));
      if (token) {
        // 获取用户信息
        this.asyncUpdateUserInfo();
      } else {
        // 初始化用户信息
        this.initUserInfo();
      }
    }
  },

  // watch: {
  //   //监听路由的变化
  //   "$route.path": {
  //     handler(newVal, oldVal) {
  //       console.log("watch...", newVal, oldVal);

  //       let token = localStorage.getItem("x-auth-token");
  //       // if (token) {
  //       //   this.changIsShowLoginModal(true);
  //       // } else {
  //       //   this.changIsShowLoginModal(false);
  //       // }
  //       // this.changIsShowLoginModal(Boolean(token));
  //     },
  //   },
  // },
};
</script>
 
<style lang = "less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    text-align: center;
    .right {
      li {
        float: left;
        margin-left: 20px;
        cursor: pointer;
        &.logo-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          s {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background: #fd604d;
          }
        }
        img {
          width: 23px;
          height: 22px;
          border-radius: 13px;
          vertical-align: middle;
        }
      }
    }
  }
}
</style>